#!/bin/bash

RED='\033[1;31m'
GREEN='\033[1;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 获取当前脚本所在的目录，处理相对路径和符号链接
SCRIPT_PATH=$(readlink -f "$0")
SCRIPT_DIR=$(dirname "$SCRIPT_PATH")

# 项目根目录
ROOT_DIR=$(cd "$SCRIPT_DIR/.." && pwd)

# 配置文件路径
CONFIG_FILE="$ROOT_DIR/etc/config.yaml"

# 日志目录
LOG_DIR="$ROOT_DIR/logs"
mkdir -p "$LOG_DIR"

# 临时文件目录
TMP_DIR="$ROOT_DIR/tmp"
mkdir -p "$TMP_DIR"

# 检查配置文件是否存在
if [ ! -f "$CONFIG_FILE" ]; then
  echo -e "${RED}配置文件不存在: $CONFIG_FILE${NC}"
  exit 1
fi

# 服务列表
SERVICES=("agent-server" "alert-server" "api-server" "log-server")

# 启动服务
start_service() {
  SERVICE_NAME=$1
  SERVICE_BIN="$SCRIPT_DIR/$SERVICE_NAME"
  LOG_FILE="$LOG_DIR/$SERVICE_NAME.log"

  if [ ! -f "$SERVICE_BIN" ]; then
    echo -e "${RED}服务二进制文件不存在: $SERVICE_BIN${NC}"
    exit 1
  fi

  echo -e "${GREEN}启动服务: $SERVICE_NAME${NC}"
  "$SERVICE_BIN" -f "$CONFIG_FILE" > "$LOG_FILE" 2>&1 &
  echo $! > "$TMP_DIR/$SERVICE_NAME.pid"

  # 确认服务是否启动成功
  sleep 2
  PID=$(cat "$TMP_DIR/$SERVICE_NAME.pid")
  if ps -p "$PID" > /dev/null 2>&1; then
    echo -e "${GREEN}服务启动成功: $SERVICE_NAME (PID: $PID)${NC}"
  else
    echo -e "${RED}服务启动失败: $SERVICE_NAME${NC}"
    rm "$TMP_DIR/$SERVICE_NAME.pid"
  fi
}

# 停止服务
stop_service() {
  SERVICE_NAME=$1
  PID_FILE="$TMP_DIR/$SERVICE_NAME.pid"

  if [ ! -f "$PID_FILE" ]; then
    echo -e "${YELLOW}PID文件不存在: $PID_FILE${NC}"
    return
  fi

  PID=$(cat "$PID_FILE")
  echo -e "${GREEN}停止服务: $SERVICE_NAME (PID: $PID)${NC}"
  kill "$PID"

  # 确认服务是否停止成功
  sleep 2
  if ps -p "$PID" > /dev/null 2>&1; then
    echo -e "${RED}服务停止失败: $SERVICE_NAME (PID: $PID)${NC}"
  else
    echo -e "${GREEN}服务停止成功: $SERVICE_NAME${NC}"
    rm "$PID_FILE"
  fi
}

# 重启服务
restart_service() {
  SERVICE_NAME=$1
  stop_service "$SERVICE_NAME"
  start_service "$SERVICE_NAME"
}

# 显示使用提示
show_usage() {
  echo -e "${YELLOW}使用方法: $(basename $0) {start|stop|restart|status} {all|service_name}${NC}"
  echo "命令:"
  echo "  start    启动服务"
  echo "  stop     停止服务"
  echo "  restart  重启服务"
  echo "  status   查看服务状态"
  echo "服务名称:"
  echo "  all           对所有服务执行操作"
  echo "  agent-server  代理服务"
  echo "  alert-server  告警服务"
  echo "  api-server    API服务"
  echo "  log-server    日志服务"
}

# 查看服务状态
status_service() {
  SERVICE_NAME=$1
  PID_FILE="$TMP_DIR/$SERVICE_NAME.pid"

  if [ ! -f "$PID_FILE" ]; then
    echo -e "${YELLOW}服务未运行: $SERVICE_NAME${NC}"
    return
  fi

  PID=$(cat "$PID_FILE")
  if ps -p "$PID" > /dev/null 2>&1; then
    echo -e "${GREEN}服务正在运行: $SERVICE_NAME (PID: $PID)${NC}"
  else
    echo -e "${YELLOW}服务未运行: $SERVICE_NAME${NC}"
    rm "$PID_FILE"
  fi
}

# 主逻辑
if [ $# -lt 2 ]; then
  show_usage
  exit 1
fi

COMMAND=$1
SERVICE=$2

case $COMMAND in
  start)
    if [ "$SERVICE" = "all" ]; then
      for service in "${SERVICES[@]}"; do
        start_service "$service"
      done
    else
      start_service "$SERVICE"
    fi
    ;;
  stop)
    if [ "$SERVICE" = "all" ]; then
      for service in "${SERVICES[@]}"; do
        stop_service "$service"
      done
    else
      stop_service "$SERVICE"
    fi
    ;;
  restart)
    if [ "$SERVICE" = "all" ]; then
      for service in "${SERVICES[@]}"; do
        restart_service "$service"
      done
    else
      restart_service "$SERVICE"
    fi
    ;;
  status)
    if [ "$SERVICE" = "all" ]; then
      for service in "${SERVICES[@]}"; do
        status_service "$service"
      done
    else
      status_service "$SERVICE"
    fi
    ;;
  *)
    show_usage
    exit 1
    ;;
esac
